package com.chuanjiabao.wisdom.ui.activity;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.adapter.ColorAdapter;
import com.chuanjiabao.wisdom.bean.ColorBean;
import com.chuanjiabao.wisdom.bean.RefreshMessageEvent;
import com.chuanjiabao.wisdom.databinding.ActivityUpdateTextBinding;
import com.chuanjiabao.wisdom.listener.OnItemClickListener;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.ui.view.TaoLinear;
import com.chuanjiabao.wisdom.ui.view.editutil.CustomHtml;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.ToastUtil;
import com.lzy.okgo.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * <p>
 * Typeface.BOLD        粗体
 * Typeface.BOLD_ITALIC 粗斜体
 * Typeface.ITALIC      斜体
 * Typeface.NORMAL      常规
 */
public class UpdateTextActivity extends BaseActivity {

    ActivityUpdateTextBinding binding;
    String homeId, relation = "", type;
    boolean isBold = true, isItalic = true, isLine = false, isColor = true;

    ColorAdapter adapter;
    List<ColorBean> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_update_text);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        homeId = getIntent().getStringExtra("module_homeId");
        relation = getIntent().getStringExtra("module_peopleId");
        type = getIntent().getStringExtra("module_type");

        list.add(new ColorBean("#FFFFFF"));
        list.add(new ColorBean("#5337E9"));
        list.add(new ColorBean("#2C3749"));
        list.add(new ColorBean("#000000"));
        list.add(new ColorBean("#999999"));
        list.add(new ColorBean("#FF4081"));
        list.add(new ColorBean("#F3CA48"));
        list.add(new ColorBean("#67B493"));

        TaoLinear taoLinear = new TaoLinear(this, 9);
        taoLinear.setScrollEnabled(false);
        adapter = new ColorAdapter(this, list);
        binding.recycler.setLayoutManager(taoLinear);
        binding.recycler.setAdapter(adapter);
        adapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                binding.etModulePwd.setFontColor(Color.parseColor(list.get(position).getColor()));
            }
        });

        binding.tvModuleLine.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        binding.etModulePwd.setFontColor(Color.parseColor("#FFFFFF"));

        binding.tvModuleCancel.setOnClickListener(this);
        binding.tvModuleSend.setOnClickListener(this);
        binding.tvModuleB.setOnClickListener(this);
        binding.tvModuleH.setOnClickListener(this);
        binding.tvModuleLine.setOnClickListener(this);
        binding.llModuleColor.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llModuleColor:
                if (isColor) {
                    isColor = false;
                    binding.recycler.setVisibility(View.VISIBLE);
                    binding.ivModuleColor.setImageResource(R.mipmap.eglass_color_red);
                } else {
                    isColor = true;
                    binding.recycler.setVisibility(View.GONE);
                    binding.ivModuleColor.setImageResource(R.mipmap.eglass_color);
                    binding.etModulePwd.setFontColor(Color.parseColor("#FFFFFF"));
                }
                break;
            case R.id.tvModuleCancel:
                finish();
                break;
            case R.id.tvModuleSend:
                String sendInfo = binding.etModulePwd.getText().toString().trim();
                if (TextUtils.isEmpty(sendInfo)) {
                    ToastUtil.showToast("请输入文本");
                    return;
                }
                String contentSecond = CustomHtml.toHtml(binding.etModulePwd.getEditableText(), CustomHtml.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE);
                if ("secret".equals(type)) {
                    addSecretText(contentSecond);
                } else {
                    if (TextUtils.isEmpty(relation)) {
                        relation = "";
                    }
                    addText(homeId, contentSecond, relation);
                }
                break;
            case R.id.tvModuleB:
                if (isBold) {
                    isBold = false;
                    binding.etModulePwd.setBold(true);
                    binding.tvModuleB.setTextColor(getResources().getColor(R.color.DB46E));
                } else {
                    isBold = true;
                    binding.etModulePwd.setBold(false);
                    binding.tvModuleB.setTextColor(getResources().getColor(R.color.white));
                }
                break;
            case R.id.tvModuleH:
                if (isItalic) {
                    isItalic = false;
                    binding.etModulePwd.setItalic(true);
                    binding.tvModuleH.setTextColor(getResources().getColor(R.color.DB46E));
                } else {
                    isItalic = true;
                    binding.etModulePwd.setItalic(false);
                    binding.tvModuleH.setTextColor(getResources().getColor(R.color.white));
                }
                break;
            case R.id.tvModuleLine:
                if (!isLine) {
                    isLine = true;
                    binding.etModulePwd.setUnderline(isLine);
                    binding.tvModuleLine.setTextColor(getResources().getColor(R.color.DB46E));
                } else {
                    isLine = false;
                    binding.etModulePwd.setUnderline(isLine);
                    binding.tvModuleLine.setTextColor(getResources().getColor(R.color.white));
                }
                break;

            default:
        }
    }

    public void addText(String homeId, String context, String relation) {
        HTTPAPI.getInstance().addText(homeId, context, relation, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                ToastUtil.showToast("提交成功");
                String from = getIntent().getStringExtra("module_from");
                if (from != null) {
                    switch (from) {
                        case "detail":
                            RefreshMessageEvent eventText = new RefreshMessageEvent();
                            eventText.setType("detail");
                            eventText.setId("4");
                            EventBus.getDefault().post(eventText);
                            break;
                    }
                }
                finish();
            }
        });
    }

    public void addSecretText(String content) {
        HTTPAPI.getInstance().addSecretText(content, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                ToastUtil.showToast("提交成功");
                finish();
            }
        });
    }

}
